package cc.chinagps.gateway.client.packet.base;

import cc.chinagps.gateway.client.packet.BasePacket;
import cc.chinagps.gateway.client.packet.PacketDecoder;
import cc.chinagps.gateway.client.packet.PacketEncoder;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.ToString;

@Data
public abstract class PacketAdapter implements PacketEncoder, PacketDecoder {
    @ToString.Exclude
    protected BasePacket basePacket;

    public PacketAdapter() {
    }

    public PacketAdapter(BasePacket basePacket) {
        this.basePacket = basePacket;
    }

    public ByteBuf encode() throws Exception {
        byte[] data = encodeBussinessData();
        basePacket.setMsgBody(data);
        return basePacket.encode();
    }

    public void decode() throws Exception {
        decodeBusinessData();
    }

    public abstract byte[] encodeBussinessData() throws Exception;

    public abstract void decodeBusinessData() throws Exception;
}
